home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / shllutil.lha / shellutils-1.8 / src / date.c < prev    next >
C/C++ Source or Header  |  1992-05-11  |  5KB  |  191 lines

  1. /* date - print or set the system date and time
  2.    Copyright (C) 1989, 1991 Free Software Foundation, Inc.
  3.  
  4.    This program is free software; you can redistribute it and/or modify
  5.    it under the terms of the GNU General Public License as published by
  6.    the Free Software Foundation; either version 2, or (at your option)
  7.    any later version.
  8.  
  9.    This program is distributed in the hope that it will be useful,
  10.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.    GNU General Public License for more details.
  13.  
  14.    You should have received a copy of the GNU General Public License
  15.    along with this program; if not, write to the Free Software
  16.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  17.  
  18. /* Options:
  19.    -d DATESTR    Display the date DATESTR.
  20.    -s DATESTR    Set the date to DATESTR.
  21.    -u        Display or set the date in universal instead of local time.
  22.    +FORMAT    Specify custom date output format, described below.
  23.    MMDDhhmm[[CC]YY][.ss]    Set the date in the format described below.
  24.  
  25.    If one non-option argument is given, it is used as the date to which
  26.    to set the system clock, and must have the format:
  27.    MM    month (01..12)
  28.    DD    day in month (01..31)
  29.    hh    hour (00..23)
  30.    mm    minute (00..59)
  31.    CC    first 2 digits of year (optional, defaults to current) (00..99)
  32.    YY    last 2 digits of year (optional, defaults to current) (00..99)
  33.    ss    second (00..61)
  34.  
  35.    If a non-option argument that starts with a `+' is specified, it
  36.    is used to control the format in which the date is printed; it
  37.    can contain any of the `%' substitutions allowed by the strftime
  38.    function.  A newline is always added at the end of the output.
  39.  
  40.    David MacKenzie <djm@gnu.ai.mit.edu> */
  41.  
  42. #include <stdio.h>
  43. #include <getopt.h>
  44. #include <sys/types.h>
  45. #include "system.h"
  46.  
  47. /* This is portable and avoids bringing in all of the ctype stuff. */
  48. #define isdigit(c) ((c) >= '0' && (c) <= '9')
  49.  
  50. #ifdef TM_IN_SYS_TIME
  51. #include <sys/time.h>
  52. #else
  53. #include <time.h>
  54. #endif
  55.  
  56. #ifndef STDC_HEADERS
  57. time_t mktime ();
  58. size_t strftime ();
  59. time_t time ();
  60. #endif
  61.  
  62. int putenv ();
  63. int stime ();
  64.  
  65. char *xrealloc ();
  66. time_t get_date ();
  67. time_t posixtime ();
  68. void error ();
  69. void show_date ();
  70. void usage ();
  71.  
  72. /* putenv string to use Universal Coordinated Time.
  73.    POSIX.2 says it should be "TZ=UCT0" or "TZ=GMT0". */
  74. #ifndef TZ_UCT
  75. #if defined(hpux) || defined(__hpux__) || defined(ultrix) || defined(__ultrix__) || defined(USG)
  76. #define TZ_UCT "TZ=GMT0"
  77. #else
  78. #define TZ_UCT "TZ="
  79. #endif
  80. #endif
  81.  
  82. /* The name this program was run with, for error messages. */
  83. char *program_name;
  84.  
  85. void
  86. main (argc, argv)
  87.      int argc;
  88.      char **argv;
  89. {
  90.   int optc;
  91.   char *datestr = NULL;
  92.   time_t when;
  93.   int set_date = 0;
  94.   int universal_time = 0;
  95.  
  96.   program_name = argv[0];
  97.  
  98.   while ((optc = getopt (argc, argv, "d:s:u")) != EOF)
  99.     switch (optc)
  100.       {
  101.       case 'd':
  102.     datestr = optarg;
  103.     break;
  104.       case 's':
  105.     datestr = optarg;
  106.     set_date = 1;
  107.     break;
  108.       case 'u':
  109.     universal_time = 1;
  110.     break;
  111.       default:
  112.     usage ();
  113.       }
  114.  
  115.   if (argc - optind > 1)
  116.     usage ();
  117.  
  118.   if (universal_time && putenv (TZ_UCT) != 0)
  119.     error (1, 0, "virtual memory exhausted");
  120.  
  121.   time (&when);
  122.  
  123.   if (datestr)
  124.     when = get_date (datestr, NULL);
  125.  
  126.   if (argc - optind == 1 && argv[optind][0] != '+')
  127.     {
  128.       when = posixtime (argv[optind]);
  129.       set_date = 1;
  130.     }
  131.  
  132.   if (when == -1)
  133.     error (1, 0, "invalid date");
  134.  
  135.   if (set_date && stime (&when) == -1)
  136.     error (0, errno, "cannot set date");
  137.  
  138.   if (argc - optind == 1 && argv[optind][0] == '+')
  139.     show_date (argv[optind] + 1, when);
  140.   else
  141.     show_date ((char *) NULL, when);
  142.  
  143.   exit (0);
  144. }
  145.  
  146. /* Display the date and/or time in WHEN according to the format specified
  147.    in FORMAT, followed by a newline.  If FORMAT is NULL, use the
  148.    standard output format (ctime style but with a timezone inserted). */
  149.  
  150. void
  151. show_date (format, when)
  152.      char *format;
  153.      time_t when;
  154. {
  155.   struct tm *tm;
  156.   char *out = NULL;
  157.   size_t out_length = 0;
  158.  
  159.   tm = localtime (&when);
  160.  
  161.   if (format == NULL)
  162.     /* Print the date in the default format.  Vanilla ANSI C strftime
  163.        doesn't support %e, but POSIX requires it.  If you don't use
  164.        a GNU strftime, make sure yours supports %e.  */
  165.     format = "%a %b %e %H:%M:%S %Z %Y";
  166.   else if (*format == '\0')
  167.     {
  168.       printf ("\n");
  169.       return;
  170.     }
  171.  
  172.   do
  173.     {
  174.       out_length += 200;
  175.       out = (char *) xrealloc (out, out_length);
  176.     }
  177.   while (strftime (out, out_length, format, tm) == 0);
  178.  
  179.   printf ("%s\n", out);
  180.   free (out);
  181. }
  182.  
  183. void
  184. usage ()
  185. {
  186.   fprintf (stderr, "\
  187. Usage: %s [-u] [-d datestr] [-s datestr] [+FORMAT] [MMDDhhmm[[CC]YY][.ss]]\n",
  188.        program_name);
  189.   exit (1);
  190. }
  191.